package rps.client.ui;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;

import rps.game.data.Figure;
import rps.game.data.Player;

public class MediaProvider {
	private static Image	
	frameIcon,									//Copyright Andreas Zipf
	mainmenu,
	about,
	gamePaneBack,								//Free for non commercial use
	rock_discovered,							//src = http://www.armyoftrolls.co.uk/website/graphics/pub_art/rock_paper.gif
	rock_discovered_red,						//Edited by Alexander Schilling
	rock_hidden,
	paper_discovered,
	paper_discovered_red,
	paper_hidden,
	scissors_discovered,
	scissors_discovered_red,
	scissors_hidden,
	flag_discovered,
	flag_discovered_red,
	flag_hidden,
	trap_discovered,
	trap_discovered_red,
	trap_hidden,
	hidden_hidden_red,
	rock_hand_own,
	paper_hand_own,
	scissors_hand_own,							//endsrc = http://www.armyoftrolls.co.uk/website/graphics/pub_art/rock_paper.gif
	possiblemove_up,							//src = http://de.all-free-download.com/frei-psd/misc/30_gratis_black_amp;_white_hand_drawn_arrows_40522.html
	possiblemove_down,
	possiblemove_left,
	possiblemove_right,
	lastmove_up,
	lastmove_down,
	lastmove_left,
	lastmove_right;								//endsrc = http://de.all-free-download.com/frei-psd/misc/30_gratis_black_amp;_white_hand_drawn_arrows_40522.html
	private static Font dotFont; 				//Fonts for non commercial use free
	private static Clip introclip, loopclip;	//Copright Andreas Zipf
	private static ImageIcon waiting;
	private static MediaProvider mediaProvider;
	
	public static MediaProvider getMediaProvider(){
		if(mediaProvider==null){
			mediaProvider = new MediaProvider();
		}
		return mediaProvider;
	}
	
	/**
	 * Returns the right Image for the given Gamebutton information
	 * @param f Figure of the button
	 * @param p Player of the game
	 * @param lastMove LastMove-Attribute of the button
	 * @param possibleMove PossibleMove-Attribute of the button
	 * @return
	 */
	public Image buttonImage(Figure f, Player p, int lastMove, int possibleMove){
		String imagename = "";
		if(f!=null){
			imagename+=f.getKind().toString()+"_";
			
			if(f.isDiscovered()) imagename+="discovered";
			else imagename+="hidden";		
						
			if(p!=null && !f.belongsTo(p)) imagename+="_red";
		}
		else if(possibleMove!=-1){			
			switch(possibleMove){
				case(0): return possiblemove_up;
				case(1): return possiblemove_down;
				case(2): return possiblemove_left;
				case(3): return possiblemove_right;
			}
		}
		else if(lastMove!=-1){
			switch(lastMove){
				case(0): return lastmove_up;
				case(1): return lastmove_down;
				case(2): return lastmove_left;
				case(3): return lastmove_right;
			}			
		}		
		return getImage(imagename);
	}
	
	private MediaProvider(){
		try{
			frameIcon = 			ImageIO.read(getClass().getResource("media/frameicon.png"));
			
			mainmenu = 				ImageIO.read(getClass().getResource("media/mainmenu.png"));
			
			about =					ImageIO.read(getClass().getResource("media/about.png"));

			waiting =               new ImageIcon(getClass().getResource("media/waiting.gif"));
			
			gamePaneBack = 			ImageIO.read(getClass().getResource("media/gamePaneBack.png"));
			
			rock_discovered = 		ImageIO.read(getClass().getResource("media/rock_discovered.png"));
			rock_discovered_red = 	ImageIO.read(getClass().getResource("media/rock_discovered_red.png"));
			rock_hidden = 			ImageIO.read(getClass().getResource("media/rock_hidden.png"));
			
			paper_discovered = 		ImageIO.read(getClass().getResource("media/paper_discovered.png"));
			paper_discovered_red = 	ImageIO.read(getClass().getResource("media/paper_discovered_red.png"));
			paper_hidden = 			ImageIO.read(getClass().getResource("media/paper_hidden.png"));
			
			scissors_discovered = 	ImageIO.read(getClass().getResource("media/scissors_discovered.png"));
			scissors_discovered_red = ImageIO.read(getClass().getResource("media/scissors_discovered_red.png"));
			scissors_hidden = 		ImageIO.read(getClass().getResource("media/scissors_hidden.png"));
			
			flag_discovered = 		ImageIO.read(getClass().getResource("media/flag_discovered.png"));
			flag_discovered_red = 	ImageIO.read(getClass().getResource("media/flag_discovered_red.png"));
			flag_hidden = 			ImageIO.read(getClass().getResource("media/flag_hidden.png"));
			
			trap_discovered = 		ImageIO.read(getClass().getResource("media/trap_discovered.png"));
			trap_discovered_red = 	ImageIO.read(getClass().getResource("media/trap_discovered_red.png"));
			trap_hidden = 			ImageIO.read(getClass().getResource("media/trap_hidden.png"));
			
			hidden_hidden_red = 	ImageIO.read(getClass().getResource("media/hidden_hidden_red.png"));
			
			rock_hand_own = 		ImageIO.read(getClass().getResource("media/rock_hand_own.png"));
			
			paper_hand_own = 		ImageIO.read(getClass().getResource("media/paper_hand_own.png"));
			
			scissors_hand_own = 	ImageIO.read(getClass().getResource("media/scissors_hand_own.png"));
			
			possiblemove_up	= 		ImageIO.read(getClass().getResource("media/possiblemove_up.png"));
			possiblemove_down = 	ImageIO.read(getClass().getResource("media/possiblemove_down.png"));
			possiblemove_left = 	ImageIO.read(getClass().getResource("media/possiblemove_left.png"));
			possiblemove_right = 	ImageIO.read(getClass().getResource("media/possiblemove_right.png"));

			lastmove_up =			ImageIO.read(getClass().getResource("media/lastmove_up.png"));
			lastmove_down =			ImageIO.read(getClass().getResource("media/lastmove_down.png"));
			lastmove_left =			ImageIO.read(getClass().getResource("media/lastmove_left.png"));
			lastmove_right =		ImageIO.read(getClass().getResource("media/lastmove_right.png"));			
			
			String osname = System.getProperty("os.name", "generic").toLowerCase();
			if (!osname.startsWith("linux")) {
				dotFont = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("media/font/dotFont.ttf")).deriveFont(60f);
			}			
			
			loopclip = AudioSystem.getClip();
			loopclip.open(AudioSystem.getAudioInputStream(getClass().getResource("media/music/loop.wav")));
			introclip = AudioSystem.getClip();
			introclip.open(AudioSystem.getAudioInputStream(getClass().getResource("media/music/intro.wav")));
			introclip.addLineListener(new LineListener() {				
				@Override
				public void update(LineEvent le) {
					if(le.getType().equals(LineEvent.Type.STOP))
						loopclip.loop(Clip.LOOP_CONTINUOUSLY);
				}
			});						
		}
		catch (FontFormatException e) {
				System.err.println("Bad Font File.");
		}
		catch(IOException e){
			System.err.println("Bad Media Import.");
        }
		catch(LineUnavailableException e){
			System.err.println("Sound File Problem.");
		}
		catch(UnsupportedAudioFileException e){
			System.err.println("Unsupported Audio File.");
		}
	}
	
	public Image getImage(String s){
		if(s.equalsIgnoreCase("frameIcon")) 					return frameIcon;
		
		else if(s.equalsIgnoreCase("mainmenu"))					return mainmenu;
		
		else if(s.equalsIgnoreCase("about")) 					return about;
		
		else if(s.equalsIgnoreCase("gamePaneBack")) 			return gamePaneBack;
		
		else if (s.equalsIgnoreCase("rock_discovered")) 		return rock_discovered;
		else if (s.equalsIgnoreCase("rock_discovered_red")) 	return rock_discovered_red;
		else if (s.equalsIgnoreCase("rock_hidden")) 			return rock_hidden;
		
		else if (s.equalsIgnoreCase("paper_discovered")) 		return paper_discovered;
		else if (s.equalsIgnoreCase("paper_discovered_red")) 	return paper_discovered_red;
		else if (s.equalsIgnoreCase("paper_hidden")) 			return paper_hidden;
		
		else if (s.equalsIgnoreCase("scissors_discovered")) 	return scissors_discovered;
		else if (s.equalsIgnoreCase("scissors_discovered_red")) return scissors_discovered_red;
		else if (s.equalsIgnoreCase("scissors_hidden")) 		return scissors_hidden;
		
		else if (s.equalsIgnoreCase("flag_discovered")) 		return flag_discovered;
		else if (s.equalsIgnoreCase("flag_discovered_red")) 	return flag_discovered_red;
		else if (s.equalsIgnoreCase("flag_hidden")) 			return flag_hidden;
		
		else if (s.equalsIgnoreCase("trap_discovered")) 		return trap_discovered;
		else if (s.equalsIgnoreCase("trap_discovered_red")) 	return trap_discovered_red;
		else if (s.equalsIgnoreCase("trap_hidden")) 			return trap_hidden;
		
		else if (s.equalsIgnoreCase("hidden_hidden_red")) 		return hidden_hidden_red;
		
		else if (s.equalsIgnoreCase("rock_hand_own")) 			return rock_hand_own;
		
		else if (s.equalsIgnoreCase("paper_hand_own"))			return paper_hand_own;
		
		else if (s.equalsIgnoreCase("scissors_hand_own")) 		return scissors_hand_own;
		else return null;	
	}
	public ImageIcon getWaitingIcon(){
		return waiting;
	}
	public Font getFont(String s){
		if(s.equalsIgnoreCase("dotFont")) return dotFont;
		else return null;		
	}
	
	public void playMusic(boolean play){
		if(play) introclip.start();
		else{
			introclip.stop();
			loopclip.stop();
			introclip.setFramePosition(0);			
			loopclip.setFramePosition(0);
		}
	}
	
	public boolean isPlayingMusic(){
		return introclip.isRunning() || loopclip.isRunning();
	}
}
